<?php
if(!defined('SYSTEM_PATH')) die('Cannot access this page directly or see your configuration file ') ;
/**
 * mysqlQuery
 *
 * @package Cosmo
 * @author menTiss
 *
 * @version 1.0
 * @access public
 */
class mysqlQuery extends Error implements DatabaseDriver {
	private $_link 	= null ;
	private $config 	= array() ;
	private $_lastQuery = null ;

	
	/**
	 * mysqlQuery::__construct()
	 * 
	 * @desc Construct the driver
	 * 
	 * @param string $config
	 */
	public function __construct($config) {
		parent::__construct() ;
		$this->config = $config ;
		$this->connect() ;
	}
	/**
	 * mysqlQuery::connect()
	 * 
	 * @desc Open a connexion to a database
	 * 
	 * @return void 
	 */
	public function connect() {
		$this->_link = @mysql_connect($this->config['server'],$this->config['username'],$this->config['password']) ;
		if(!$this->_link) {
			$this->setError($this->error(), true) ;
		} else {
			$result = @mysql_select_db($this->config['database'],$this->_link) ;
			if(!$result) $this->setError($this->error(), true) ;
		}
	}
	/**
	 * mysqlQuery::query()
	 * 
	 * @desc Execute a sql code on the database
	 * 
	 * @param string $sql
	 * 
	 * @return resource
	 */
	public function query($sql) {
		$this->_lastQuery = $sql ;
		$query = @mysql_query($sql,$this->_link) ;
		if(!$query) $this->setError($this->error(), true) ;
		return $query ;
	}
	/**
	 * mysqlQuery::close()
	 * 
	 * @desc Close database connexion
	 * 
	 * @return bool
	 */
	public function close() {
		return @mysql_close($this->_link) ;
	}
	/**
	 * mysqlQuery::getLink()
	 * 
	 * @desc Return the identifier link used in the driver class
	 * 
	 * @return resource
	 */
	public function getLink() {
		return $this->_link ;
	}
	/**
	 * mysqlQuery::error()
	 * 
	 * @desc return the last error message
	 * 
	 * @return string
	 */
	public function error() {
		return mysql_error() ;
	}
	/**
	 * mysqlQuery::__call()
	 * 
	 * @desc Allow to execute non listed function
	 * 
	 * @param string $method
	 * @param mixed $params
	 * 
	 * @return 
	 */
	public function __call($method,$params) { //
	/*
	NOTE : Maybe i should not use this function to have a full control on function called, to make
	difference between queries and results
	*/
		if(function_exists('mysql_'.$method)) {
			$res = @call_user_func_array('mysql_'.$method, $params) ;
			if(!$res) $this->setError($this->error(), true) ;
			return $res ;
		}
		return null ;
	}
	/**
	 * mysqlQuery::getDriverType()
	 *
	 * @desc Return the type of database managed by the class
	 *  
	 * @return string
	 */
	public function getDriverType() {
		return 'mysql' ;
	}
	/**
	 * mysqlQuery::getLastQuery()
	 * 
	 * @desc Return the last sql query
	 * 
	 * @return string
	 */
	public function getLastQuery() {
		return $this->_lastQuery ;
	}
	/**
	 * mysqlQuery::getLastId()
	 * 
	 * @desc Return last id from an auto_increment field after an insert
	 * 
	 * @return int
	 */
	public function getLastId() {
		return @mysql_insert_id();
	}
	/**
	 * mysqlQuery::getFields()
	 * 
	 * @desc Return table's fields list
	 * 
	 * @param string $table
	 * @return array 
	 */
	public function getFields($table) {
		$sql = "SELECT * FROM ".$table." LIMIT 1";
		$result = $this->query($sql) ;
		$numField = @mysql_num_fields($result);
		$fields = array() ;
		for ($i=0; $i < $numField; $i++) {
			$fields[@mysql_field_name($result, $i)] = array(
				'name'	=> @mysql_field_name($result, $i),
				'type' 	=> @mysql_field_type($result, $i),
				'len'   	=> @mysql_field_len($result, $i),
    				'flags' 	=> @mysql_field_flags($result, $i),
				);
		}
		@mysql_free_result($result);
		return $fields;
	}
}
?>